<%@ page language="java" contentType="text/html;charset=GBK"%>
<%@ page import="java.lang.*"%>
<%@ page import="java.sql.*"%>
<%@ page import="edu.zju.SensorDataFetcher"%>
<%@ page import="java.util.Map"%>
<%@ page import="edu.zju.PortFinder"%>

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title></title>
<link href="jquery/dist/jquery.jqplot.min.css" rel="stylesheet" />
<script src="jquery/dist/jquery.min.js"></script>
<script src="jquery/dist/jquery.jqplot.min.js"></script>
<script src="jquery/dist/excanvas.min.js"></script>
<script src="jquery/dist/plugins/jqplot.barRenderer.min.js"></script>
<script src="jquery/dist/plugins/jqplot.pointLabels.min.js"></script>
<script src="jquery/dist/plugins/jqplot.canvasAxisTickRenderer.min.js"></script>
<script src="jquery/dist/plugins/jqplot.cursor.min.js"></script>
<script src="jquery/dist/plugins/jqplot.highlighter.min.js"></script>
<script src="jquery/js/jqplot/plugins/jqplot.dateAxisRenderer.min.js"></script>
<script src="jquery/dist/plugins/jqplot.canvasTextRenderer.min.js"></script>
<script src="jquery/dist/plugins/jqplot.categoryAxisRenderer.min.js"></script>

<script src="js/showdata.js"></script>
</head>
<body>
	<%
		request.setCharacterEncoding("utf-8");
		String txtMsg = request.getParameter("search");
		int nodeid = Integer.parseInt(txtMsg) + 1; //FIXME:Should all of code +1 or ?
		String realPath = application.getRealPath("/");
		PortFinder finder = new PortFinder(realPath);

		Map<String, String> portMap = finder.find();
		String usbPort = portMap.get(nodeid + "");
		System.out.println("Showdata: finding port of " + nodeid + ",result is " + usbPort);
		if (usbPort == null || usbPort.equals("")) {
			out.println("<script>alert('ThIS NODE IS NOT CONNECTED');"
					+"history.go(-1);"
					+ "</script>");
		} else {

			SensorDataFetcher fetcher = new SensorDataFetcher();
			Map map = fetcher.selectSensorData(usbPort);
			request.setAttribute("lightdata", map.get("light"));
			request.setAttribute("humdata", map.get("hum"));
			request.setAttribute("temdata", map.get("tem"));
			request.setAttribute("timedata", map.get("time"));

			String timeInterval = "2000";
			out.println("<script> var timerId = setInterval(function() { window.location.reload();}, +"
					+ timeInterval + ");</script>");
		}
	%>

	<input type="hidden" name="inputlightdata" id="inputlightdata"
		value="${lightdata}">
	<input type="hidden" name="inputtemdata" id="inputtemdata"
		value="${temdata}">
	<input type="hidden" name="inputhumdata" id="inputhumdata"
		value="${humdata}">
	<input type="hidden" name="inputtimedata" id="inputtimedata"
		value="${timedata}">

	<div></div>
	<div id="chart1" style="height: 180px; width: 750px;"></div>
	<div id="chart2" style="height: 180px; width: 750px;"></div>
	<div id="chart3" style="height: 180px; width: 750px;"></div>
</body>
</html>
